import Mock from "mockjs";
import { param2Obj } from "../utils/myUtils";
let list = [];
for (var i = 0; i < 8; i++) {
  list.push({
    id: Mock.mock("@guid"),
    name: Mock.mock("@cname"),
    address: Mock.mock("@county(true)"),
    date: Mock.mock("@datetime"),
    type: Mock.mock("@integer(0, 2)"),
    income: Mock.mock("@integer(1000, 10000)"),
    pay: Mock.mock("@integer(1000, 10000)"),
    money: Mock.mock("@integer(1000, 10000)"),
  });
}

export default {
  getHomeData: (config) => {
    console.log("config", param2Obj(config.url));
    let { search } = param2Obj(config.url);
    let newlist = list.filter((item) => {
      return item.name.indexOf(search) != -1;
    });
    return {
      code: 200,
      msg: "请求成功",
      data: newlist,
    };
  },
  deleteMockData: (config) => {
    let { id } = JSON.parse(config.body);
    let newlist = list.filter((item) => {
      return item.id != id;
    });
    list = newlist;
    return {
      code: 200,
      msg: "删除成功",
      data: null,
    };
  },
  batchdelet: (config) => {
    let { ids } = JSON.parse(config.body);
    let newlist = list.filter((item) => {
      return ids.indexOf(item.id) === -1;
    });
    list = newlist;
    return {
      code: 200,
      msg: "批量删除成功",
      data: null,
    };
  },
  addmockdata: (config) => {
    let { name, address, type, income, pay, money } = JSON.parse(config.body);
    list.push({
      id: Mock.mock("@guid"),
      name: name,
      address: address,
      date: Mock.mock("@datetime"),
      type: type,
      income: income,
      pay: pay,
      money: money,
    });
    return {
      code: 200,
      msg: "添加成功",
      data: null,
    };
  },
};
